package ngames.com.sushitv.core;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;

import java.util.ArrayList;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.action.SearchAction;
import ngames.com.sushitv.api.Api;
import ngames.com.sushitv.api.ApiImplOKHttp;
import ngames.com.sushitv.api.ApiResponse;
import ngames.com.sushitv.api.JSONResolve;
import ngames.com.sushitv.entity.Anime;
import ngames.com.sushitv.entity.AnimeIntro;
import ngames.com.sushitv.entity.ButtonHome;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.PromptUser;
import ngames.com.sushitv.entity.SearchPrompt;
import ngames.com.sushitv.entity.SearchRequestAll;
import ngames.com.sushitv.entity.Tag;
import ngames.com.sushitv.entity.UpUser;
import ngames.com.sushitv.entity.UserInfo;
import ngames.com.sushitv.entity.VideoBase;
import ngames.com.sushitv.entity.VideoDetails;
import ngames.com.sushitv.entity.genres;
import ngames.com.sushitv.interfaces.TransmitSearchPrompt;
import ngames.com.sushitv.net.okhttp.callback.RequestCallBack;
import ngames.com.sushitv.net.okhttp.callback.RequestCallBackJSON;
import okhttp3.Response;

/**
 * Created by Jeffrey on 2016/7/26.
 */
public class AppModelImpl implements AppModel {
    private Context mContext;
    private Activity mCurrentActivity;
    private Api mApiImpl;
    private UserService mUserService;

    public AppModelImpl(Context context) {
        mContext = context;
        mApiImpl = new ApiImplOKHttp(context);
        mUserService = new UserService(context, mApiImpl);
    }

    @Override
    public void setCurrentActivity(Activity activity) {
        mCurrentActivity = activity;
        mUserService.setCurrentActivity(mCurrentActivity);
    }

    @Override
    public UserService getUserService() {
        return mUserService;
    }

    @Override
    public UserInfo getUserInfo() {
        return mUserService.getUserInfo();
    }

    @Override
    public void cancelRequest(String urlTag) {
        mApiImpl.cancelRequest(urlTag);
    }

    @Override
    public void videoRank(String type, String cycle, String size, String lastID, final ModelCallback<ArrayList<ItemVideo>> listener) {
        if (TextUtils.isEmpty(size)) {
            size = "15";
        }
        mApiImpl.videoRank(type + "_" + cycle, size, new RequestCallBackJSON<ArrayList<ItemVideo>>() {
            @Override
            public ApiResponse<ArrayList<ItemVideo>> onParse(String responseStr) {
                return JSONResolve.resolveGSONListData(responseStr, ItemVideo[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void videoRank(String type_cycle, String size, String lastID, final ModelCallback<ArrayList<ItemVideo>> listener) {
        mApiImpl.videoRank(type_cycle, size, new RequestCallBackJSON<ArrayList<ItemVideo>>() {
            @Override
            public ApiResponse<ArrayList<ItemVideo>> onParse(String responseStr) {
                return JSONResolve.resolveGSONListData(responseStr, ItemVideo[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void videoRank(final String key, String ID, final ModelCallback<ArrayList<ItemVideo>> listener) {
        mApiImpl.videoRank(ID, new RequestCallBackJSON<ArrayList<ItemVideo>>() {
            @Override
            public ApiResponse<ArrayList<ItemVideo>> onParse(String responseStr) {
                return JSONResolve.resolveGSONListData(key, responseStr, ItemVideo[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void videoCates(final ModelCallback<ArrayList<ButtonHome>> listener) {
        mApiImpl.videoCates(new RequestCallBackJSON<ArrayList<ButtonHome>>() {
            @Override
            public ApiResponse<ArrayList<ButtonHome>> onParse(String responseStr) {
                return JSONResolve.resolveGSONListData(responseStr, ButtonHome[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<ButtonHome> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void hotword(final ModelCallback<ArrayList<String>> listener) {
        mApiImpl.hotWord(new RequestCallBackJSON<ArrayList<String>>() {
            @Override
            public ApiResponse<ArrayList<String>> onParse(String responseStr) {
                return JSONResolve.resolveGSONListData(responseStr, String[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<String> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void searchPrompt(String word, final String key, final ModelCallback<ArrayList<String>> listener, final TransmitSearchPrompt ts) {
        mApiImpl.searchPrompt(word, key, new RequestCallBackJSON<ArrayList<String>>() {
            @Override
            public ApiResponse<ArrayList<String>> onParse(String responseStr) throws JSONException {
                SearchPrompt searchPrompt = new SearchPrompt();

                ApiResponse<ArrayList<String>> s = JSONResolve.resolveGSONListData("video_titles", responseStr, String[].class);
                ArrayList<String> str = s.getObj();
                searchPrompt.setTitle(str);
                searchPrompt.setUser(JSONResolve.resolveGSONListData("users", responseStr, PromptUser[].class).getObj());
                AppApplication.setSearchPrompt(searchPrompt);
                ts.transmitSearchPrompt(searchPrompt);
                return JSONResolve.resolveGSONListData(key, responseStr, String[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<String> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void AnimeList(String order, String type, String name, String page, final ModelCallback<ArrayList<Anime>> listener) {
        mApiImpl.searchRequest(order, type, name, page, new RequestCallBackJSON<ArrayList<Anime>>() {
            @Override
            public ApiResponse<ArrayList<Anime>> onParse(String responseStr) throws JSONException {
                SearchRequestAll search = new SearchRequestAll();
                search.setAnimes(JSONResolve.resolveGSONListData("hisplay", responseStr, Anime[].class).getObj());
                search.setTags(JSONResolve.resolveGSONListData("videos", responseStr, Tag[].class).getObj());
                AppApplication.setSearchRequestAll(search);
                return JSONResolve.resolveGSONListData("hisplay", responseStr, Anime[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<Anime> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void UserList(final String type, String name, String page, final ModelCallback<ArrayList<UpUser>> listener) {
        mApiImpl.searchUserRequest(type, name, page, new RequestCallBackJSON<ArrayList<UpUser>>() {
            @Override
            public ApiResponse<ArrayList<UpUser>> onParse(String responseStr) throws JSONException {

                return JSONResolve.resolveGSONListData(type, responseStr, UpUser[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<UpUser> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void videoDetails(String vsId, final ModelCallback<ArrayList<ItemVideo>> listener) {
        mApiImpl.videoDetails(vsId, new RequestCallBackJSON<ArrayList<ItemVideo>>() {
            @Override
            public ApiResponse<ArrayList<ItemVideo>> onParse(String responseStr) throws JSONException {
                VideoDetails videoDetails = new VideoDetails();
                videoDetails.setBase(JSONResolve.resolveGSONData("base", responseStr, VideoBase.class).getObj());
                videoDetails.setUser(JSONResolve.resolveGSONData("user", responseStr, UpUser.class).getObj());
                videoDetails.setRecommends(JSONResolve.resolveGSONListData("recommends", responseStr, ItemVideo[].class).getObj());
                videoDetails.setSeries(JSONResolve.resolveGSONListData("series", responseStr, ItemVideo[].class).getObj());
                AppApplication.setmVideoDetails(videoDetails);
                return JSONResolve.resolveGSONListData("series", responseStr, ItemVideo[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void animeDetails(String vId, final ModelCallback<ArrayList<ItemVideo>> listener) {
        mApiImpl.animeDetails(vId, new RequestCallBackJSON<ArrayList<ItemVideo>>() {
            @Override
            public ApiResponse<ArrayList<ItemVideo>> onParse(String responseStr) throws JSONException {
                AnimeIntro animeIntro=new AnimeIntro();
                animeIntro.setAnime(JSONResolve.resolveGSONData(responseStr, Anime.class).getObj());
                animeIntro.setGenres(JSONResolve.resolveGSONListData("genres", responseStr, genres[].class).getObj());
                animeIntro.setSeries(JSONResolve.resolveGSONListData("series", responseStr, ItemVideo[].class).getObj());
                AppApplication.setmAnimeIntro(animeIntro);
                return JSONResolve.resolveGSONListData("series", responseStr, ItemVideo[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> obj) {
                listener.onSuccess(obj);
            }
        });
    }

    @Override
    public void ItemVideo(String id, int videoType, int total, int page, ModelCallback<ArrayList<ItemVideo>> fail) {
        mApiImpl.itemVideo(id, videoType, total, page, new RequestCallBackJSON<ArrayList<ItemVideo>>() {
            @Override
            public ApiResponse<ArrayList<ItemVideo>> onParse(String responseStr) throws JSONException {
                return JSONResolve.resolveGSONListData(responseStr,ItemVideo[].class);
            }

            @Override
            public void onFailure(String code, String msg) {

            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> obj) {

            }
        });
    }



  /*  @Override
    public void searchPrompt(String word, final String key, final ModelCallback<ArrayList<String>> listener) {
        mApiImpl.searchPrompt(word, new RequestCallBackJSON<ArrayList<SearchPrompt>>() {
            @Override
            public ApiResponse<ArrayList<SearchPrompt>> onParse(String responseStr) throws JSONException {
                return null;
            }

            @Override
            public void onFailure(String code, String msg) {

            }

            @Override
            public void onSuccess(ArrayList<SearchPrompt> obj) {

            }
        });
    }*/




  /*  @Override
    public void searchPrompt(String word, final ModelCallback<ArrayList<SearchPrompt>> listener) {
        mApiImpl.searchPrompt(word,new RequestCallBackJSON<ArrayList<SearchPrompt>>() {
            @Override
            public ApiResponse<ArrayList<SearchPrompt>> onParse(String responseStr) throws JSONException {

                SearchPrompt searchPrompt=new SearchPrompt();
                ApiResponse<ArrayList<String>> s=JSONResolve.resolveGSONListData("video_titles", responseStr, String[].class);
                ArrayList<String> str=s.getObj();
                searchPrompt.setTitle(str);
                searchPrompt.setUser(JSONResolve.resolveGSONListData("users", responseStr, String[].class).getObj());
                ApiResponse<ArrayList<SearchPrompt>> sear=new ApiResponse<ArrayList<SearchPrompt>>();
                ArrayList<SearchPrompt> search=new ArrayList<SearchPrompt>();
                search.add(searchPrompt);
                sear.setObj(search);

                return sear;
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<SearchPrompt> obj) {
                listener.onSuccess(obj);
            }

        });
    }*/

    /*@Override
    public void videoContent(ArrayList<ButtonHome> data, final ModelCallback<ArrayList<FanMadeContent>> listener) {
        mApiImpl.videoContent(data ,new RequestCallBackJSON<ArrayList<FanMadeContent>>() {
            @Override
            public ApiResponse<ArrayList<FanMadeContent>> onParse(String responseStr) {
                return null;
            }
            @Override
            public ApiResponse<ArrayList<FanMadeContent>> onParse(String responseStr) {
                return JSONResolve.resolveGSONListData(responseStr, FanMadeContent[].class);
            }

            @Override
            public void onFailure(String code, String msg) {
                listener.onFailure(code, msg);
            }

            @Override
            public void onSuccess(ArrayList<FanMadeContent> obj) {
                listener.onSuccess(obj);
            }
        });
    }*/
}
